﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FB.Bussiness;
using FB.Utilities;
using FB.DataAccess;

namespace FB.admin
{
    public partial class VideoAdd : System.Web.UI.Page
    {
       /// <summary>
        ///  View State AddItemID
        /// </summary>
        private Guid? AddItemID
        {
            get
            {
                object obj = this.ViewState["_AddItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_AddItemID"] = value;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadVideoCategory();
            }
        }
        private void CheckPermission()
        {
            if (Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR)
                || Page.User.IsInRole(Constants.ROLE_AUTHOR)
                || Page.User.IsInRole(Constants.ROLE_CONTRIBUTER)
                || Page.User.IsInRole(Constants.ROLE_EDITOR))
            {
                //
            }
            else
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }

        private void LoadVideoCategory()
        {
            VideoCategoryBO BO = new VideoCategoryBO();
            chklVideoCategory.DataSource = BO.GetAllVideoCategory();
            chklVideoCategory.DataTextField = "Name";
            chklVideoCategory.DataValueField = "ID";
            chklVideoCategory.DataBind();
        }

        private void LoadYoutubeInfomation(YouTubeVideoObject youtubeVideo)
        {
            lblTitle.Text = "Video: Add";
            if (youtubeVideo != null)
            {
                string TagsString = youtubeVideo.GetTagsString();
                txtDescription.Text = youtubeVideo.Description;
                txtTags.Text = TagsString;
                txtTitle.Text = youtubeVideo.Title;
                chkEnable.Checked = false;
                //Load Youtube properties
                txtYAuthor.Text = youtubeVideo.Author;
                txtYChanel.Text = Convert.ToString(youtubeVideo.ChanelID);
                txtYDescription.Text = youtubeVideo.Description;
                txtYDisliked.Text = "";
                txtYDuration.Text = Convert.ToString(youtubeVideo.Duration);
                txtYLiked.Text = "";
                txtYRatingAverange.Text = Convert.ToString(youtubeVideo.RatingAverage);
                txtYTitle.Text = youtubeVideo.Title;
                txtYVideoID.Text = youtubeVideo.VideoId;
                txtYViewCount.Text = Convert.ToString(youtubeVideo.ViewCount);
                txtYTags.Text = TagsString;
                imgThumbail.ImageUrl = Utilities.Common.GetYoutubeThumb(youtubeVideo.VideoId);
            }
        }


        protected void btnGetVideo_Click(object sender, EventArgs e)
        {
            VideoBO BO = new VideoBO();
            YouTubeVideoObject FeedVideo = BO.FeedVideoByYoutubeID(txtVideoID.Text);
            if (FeedVideo != null)
            {
                LoadYoutubeInfomation(FeedVideo);
            }
            else
            {
                Response.Write(Utilities.Common.FormatAlertMsg("Video doesn't exist !"));
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveForm();
            //TODO: Show msg
            Response.Redirect("VideoEdit.aspx?ID=" + AddItemID);
        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveForm();
            Response.Redirect("VideoList.aspx");
        }
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            SaveForm();
            AddItemID = null;
            Response.Redirect("VideoAdd.aspx?ID=0");
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("VideoList.aspx");
        }
        private void SaveForm()
        {
            //Collec infomation
            Video NewVideo = new Video();
            NewVideo.ID = Guid.NewGuid();
            NewVideo.Description = txtDescription.Text;
            NewVideo.Enable = chkEnable.Checked;
            NewVideo.Author = txtYAuthor.Text;
            NewVideo.ChanelID = Guid.Parse("ac5ddc8e-cb1e-4ae2-83e0-4dfe32ec6d16");// chanel default of danh cho thieu nhi
            Decimal DurationTemp;
            if (Decimal.TryParse(txtYDuration.Text, out DurationTemp))
            {
                NewVideo.Duration = DurationTemp;
            }
            NewVideo.Private = chkYPrivate.Checked;
            NewVideo.Title = txtYTitle.Text;
            NewVideo.UDescription = txtYDescription.Text;
            NewVideo.URatingAverage = Convert.ToDecimal(txtYRatingAverange.Text);
            NewVideo.UViewCount = Convert.ToDecimal(txtYViewCount.Text);
            NewVideo.YoutubeID = txtYVideoID.Text;
            NewVideo.CreatedDate = DateTime.Now;
            NewVideo.LastCheckAliveDate = DateTime.Now;
            NewVideo.ThumbnailUrl = "http://img.youtube.com/vi/" + txtYVideoID.Text + "/1.jpg";
            
            VideoBO BO = new VideoBO();
            // Insert this video
            Video AddedVideo = BO.InsertVideoAndReturn(NewVideo).RetValue;
            if (AddedVideo == null) return; //Video already exist
            //Add category of this video
            foreach (ListItem item in chklVideoCategory.Items)
            {
                if (item.Selected)
                {
                    Video_Category video_Cat = new Video_Category();
                    video_Cat.ID = Guid.NewGuid();
                    video_Cat.IDCategory = Guid.Parse(item.Value);
                    video_Cat.IDVideo = AddedVideo.ID;
                    //Add this
                    Video_CategoryBO video_CatBO = new Video_CategoryBO();
                    video_CatBO.InsertVideo_Category(video_Cat);
                }
            }
            AddItemID = AddedVideo.ID;

            // Add tags for thí video
            //Update Tags for Video
            Video_TagBO TBO = new Video_TagBO();
            TBO.AddTagOfVideo(AddedVideo.ID, txtTags.Text);
        }
    }
}